package org.ko.core.dao.auth.impl;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.ko.core.dao.auth.MenuDao;
import org.ko.core.dao.hibernate4.BaseTypedDao;
import org.ko.core.dao.support.Finder;
import org.ko.core.domain.auth.Menu;
import org.ko.core.websupport.page.Page;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

/**
 * 系统菜单管理DAO接口实现
 * @author BQC
 * @version 2014-09-14 15:40:06
 */
@Repository
public class MenuDaoImpl extends BaseTypedDao<Menu, Long> implements MenuDao{

	@Override
	protected Class<Menu> getEntityClass() {
		return Menu.class;
	}

	@Override
	public Page findPage(int pageNo, int pageSize,String searchText) {
		Finder f = Finder.create("FROM Menu where 1=1 ");
		if(StringUtils.isNotBlank(searchText)){
			f.append(" and (name like :searchText )");
			f.setParam("searchText", "%"+searchText+"%");
		}
		f.append(" order by orderNo desc");
		return find(f, pageNo, pageSize);
	}

	@Override
	public List findTree(String searchText) {
		Finder f = Finder.create("");
		f.append("select  ")
		 .append(" id ,")
		 .append(" name ,")
		 .append(" url ,")
		 .append(" icon as iconSkin ,")
		 .append(" pid ,")
		 .append(" orderNo ,")
		 .append(" type ,")
		 .append(" remarks")
		 ;
		
		f.append(" FROM "+getTableName()+" where 1=1 ");
		if(StringUtils.isNotBlank(searchText)){
			f.append(" and (name like :searchText )");
			f.setParam("searchText", "%"+searchText+"%");
		}
		f.append(" order by orderNo desc");
		return findBySql(f, Map.class);
	}
	
	/**
	 * 查找权限菜单(包含子菜单)
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List findAuthMenus() {
		Finder f = Finder.create("");
		f.append("select  ")
		.append(" id ,")
		.append(" name ,")
		.append(" url ,")
		.append(" icon as iconSkin ,")
		.append(" pid ,")
		.append(" orderNo ,")
		.append(" type ,")
		.append(" remarks")
		;
		
		f.append(" FROM "+getTableName()+" where 1=1 ");
		f.append(" and pid is null ");
		f.append(" order by orderNo desc");
		
		List<Map> topMenus = findBySql(f, Map.class);
		for (Map tm : topMenus) {
			BigInteger pid = (BigInteger) tm.get("id");
			tm.put("sublist", findByParent(pid==null?null:pid.longValue()));
		}
		return topMenus;
	}
	/**
	 * 根据父ID查的子菜单
	 * @param pid
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List findByParent(Long pid) {
		Finder f = Finder.create("");
		f.append("select  ")
		.append(" id ,")
		.append(" name ,")
		.append(" url ,")
		.append(" icon as iconSkin ,")
		.append(" pid ,")
		.append(" orderNo ,")
		.append(" type ,")
		.append(" remarks")
		;
		
		f.append(" FROM "+getTableName()+" where 1=1 ");
		f.append(" and pid=:pid").setParam("pid", pid);
		f.append(" order by orderNo desc");
		
		return findBySql(f, Map.class);
	}

	public int changeParent(Long id, Long pid) {
		Assert.notNull(id,"ID不能为空");
		Assert.notNull(pid,"父ID不能为空");
		return updateBySql("update "+getTableName()+" set pid=? where id=?",pid, id);
	}
	
	
}